package com.ibm.cps.servlet;

import java.util.Collection;
import java.util.HashSet;

import com.ibm.cps.message.MessageFields;
import com.ibm.interfaces.IPersistency;
import com.ibm.mongo.MongoPersistency;
import com.ibm.util.ErrorCode;
import com.ibm.util.exception.CPSException;

/**
 * 
 * @author Jingjing Wang 2015-7-6
 */

public class ProcessorsGetFactory {

	public static Collection<String> getMetadatasAndGroups(String source, String tenantid, String type, String groupid,
			String processorid) throws CPSException {
		IPersistency persistency = MongoPersistency.getInstace();
		Collection<String> metadata = new HashSet<String>();
		// get metadata according to different source
		if (source != null && source.equals(MessageFields.GROUP_TYPE_SQL)) {
			if (groupid == null) {
				throw new CPSException(ErrorCode.NULL_FIELD_ERROR, "Please provide the value of groupid.");
			}
		}
		metadata = persistency.getProcessors(tenantid, processorid, type, groupid);
		Collection<String> groupMetadata = persistency.getGroupMetadata(tenantid, groupid);

		if (groupMetadata != null) {
			if (metadata == null) {
				metadata = new HashSet<String>();
			}
			metadata.addAll(groupMetadata);
		}
		return metadata;
	}

}
